<!--
 * @Author: 杨超
 * @Date: 2021-11-24 20:33:45
 * @LastEditTime: 2021-12-01 17:10:19
 * @LastEditors: 杨超
 * @Description:
-->
<template>
    <div :style="item.style || {}" :class="`form-unqiue-${item.key} ${getTextModel ? '' : 'wti-untext-box'}`">
        <el-select
            v-if="!getTextModel"
            v-model="val"
            style="width: 100%"
            :disabled="getDisabled"
            :placeholder="getSelectPlaceholder(item)"
            v-bind="bindOptions"
        >
            <el-option
                v-for="option in item.options"
                :key="option.value"
                :label="option.label"
                :value="option[item.valueKey || 'value']"
            />
        </el-select>
        <div v-else :style="item.textStyle || {}" class="form-input-text">
            {{ textModelValue || '-' }}
        </div>
    </div>
</template>

<script>
import FormMixin from './mixin'

export default {
    name: 'FormNormalSelect',
    mixins: [FormMixin],
    computed: {
        textModelValue() {
            if (this.item.showText) {
                return this.value
            }
            if (this.item.options) {
                let val = ''
                const key = this.item.valueKey || 'value'
                this.item.options.forEach(item => {
                    if (item[key] === this.value) {
                        val = item.label
                    }
                })
                return val
            } else {
                return ''
            }
        },
        val: {
            get() {
                return this.value
            },
            set(v) {
                this.$emit('input', v)
                this._valueLink(v)
                // 只有非子表单的情况下，才会冒泡上去数据变更
                if (this.formItemType !== 'childForm') {
                    this.statusChangeFn.valueUpdateEvent({
                        [this.item.key]: v
                    })
                } else {
                    // 如果是子表单的话，执行内置的变更
                    this.childChangeData.valueUpdateEvent()
                }
            }
        }
    }
}
</script>

<style scoped lang="scss"></style>
